package com.yzy.fly.loader;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzy.fly.entity.role.FlyAttribute;
import com.yzy.fly.entity.role.FlyModel;
import com.yzy.fly.entity.role.FlyInterface;
import com.yzy.fly.entity.role.FlyRequestMapping;
import com.yzy.fly.mapper.FlyAttributeMapper;
import com.yzy.fly.mapper.FlyModelMapper;
import com.yzy.fly.mapper.FlyInterfaceMapper;
import com.yzy.fly.mapper.FlyRequestMappingMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 模型加载器
 */
@Component
public class FlyModelLoader {
    @Autowired
    private FlyModelMapper flyModelMapper;
    @Autowired
    private FlyAttributeMapper flyAttributeMapper;

    public  List<FlyModel> loaderAll(int systemId) {
        List<FlyModel> flyModels = flyModelMapper.selectAllBySystemId(systemId);
        if (CollectionUtils.isNotEmpty(flyModels)) {
            for (int i = 0; i < flyModels.size(); i++) {
                FlyModel flyModel =  flyModels.get(i);
                loader(flyModel);
            }
        }
        return flyModels;
    }

    public FlyModel loader(int id) {
        return loaderRootId(id);
    }

    public FlyModel loaderRootId(int id) {
        FlyModel flyModel = flyModelMapper.selectById(id);
        return loader(flyModel);
    }

    public FlyModel loader(FlyModel flyModel) {
        if (flyModel != null) {
            Integer id = flyModel.getId();
            QueryWrapper<FlyAttribute> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("parentId",controllerId);
            queryWrapper.eq("modelId",id);
            List<FlyModel> childrenList = flyModelMapper.selectAllByParentId(id);
            if (CollectionUtils.isNotEmpty(childrenList)) {
                flyModel.setChildrens(childrenList);
            }
            List<FlyAttribute> flyAttributes = flyAttributeMapper.selectList(queryWrapper);
//            List<FlyInterface> flyInterfaces = flyInterfaceMapper.selectAllByControllerId(controllerId);

            if (CollectionUtils.isNotEmpty(flyAttributes)) {
                flyModel.setAttrs(flyAttributes);
                for (int i = 0; i < flyAttributes.size(); i++) {
                    FlyAttribute flyAttribute =  flyAttributes.get(i);
//                    List<FlyRequestMapping> flyRequestMappings = flyRequestMappingMapper.selectAllByControllerId(flyInterface.getId());
//                    flyInterface.setRequestMappings(flyRequestMappings);
                }
            }
        }
        return flyModel;
    }
}
